package com.example.list.recyleview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.list.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapterWithFooter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<TV_MSG> datas;
    Context context;
    ItemClickListener mItemClickListener ;
    private static final int TYPE_ITEM =0;  //普通Item View

    private static final int TYPE_FOOTER = 1;  //顶部FootView

    public MyAdapterWithFooter(Context context, List<TV_MSG> datas) {
        this.datas = datas;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType==TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);

            return new MyHolder(view);
        }else if(viewType==TYPE_FOOTER){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_footer, parent, false);

            return new FootViewHolder(view);
        }

        return  null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyHolder) {

            ((MyHolder) holder).tv_msg.setText(datas.get(position).getText());
            ((MyHolder) holder).tv_msg.setBackgroundColor(datas.get(position).getColor());
        }else if (holder instanceof FootViewHolder){

            ((FootViewHolder) holder).foot_view_item_tv.setText(datas.get(position).getText());
        }


        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 这里利用回调来给RecyclerView设置点击事件
                    mItemClickListener.onItemClick(position,v);
                }
            });
        }

        }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    //位置为底部的item，设置独占一行,做到新开一行，适用于刷新功能
//                    return getItemCount()-1==position? gridManager.getSpanCount(): 1;

                    //gridManager.getSpanCount()/2 对半分，有点意思
                    return getItemCount()-1==position? gridManager.getSpanCount()/2 : 1;
                }
            });
        }
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_msg;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            tv_msg = itemView.findViewById(R.id.tv_msg);
        }
    }

    /**
     * 底部FootView布局
     */
    static class FootViewHolder extends  RecyclerView.ViewHolder{
        private TextView foot_view_item_tv;
        public FootViewHolder(View view) {
            super(view);
            foot_view_item_tv=(TextView)view.findViewById(R.id.foot_view_item_tv);
        }
    }

    public interface ItemClickListener{
        void onItemClick(int position, View view) ;
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener){
       mItemClickListener = itemClickListener ;

    }

}
